<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="讲师名称">
        <el-input v-model="teacher.name" />
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number
          v-model="teacher.sort"
          controls-position="right"
          min="0"
        />
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="teacher.level" clearable placeholder="请选择">
          <!--
            数据类型一定要和取出的json中的一致，否则没法回填
            因此，这里value使用动态绑定的值，保证其数据类型是number
          -->
          <el-option :value="1" label="高级讲师" />
          <el-option :value="2" label="首席讲师" />
        </el-select>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="teacher.career" />
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="teacher.intro" :rows="10" type="textarea" />
      </el-form-item>

      <!-- 讲师头像：TODO -->
      <el-form-item label="讲师头像">
        <el-upload
          class="avatar-uploader"
          action="http://localhost/api/oss/file/upload?module=avatar"
          :show-file-list="false"
          :on-success="fileUploadSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img
            v-if="teacher.avatar"
            :src="teacher.avatar"
            style="width: 100px; height: 100px"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
        >
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import teacherApi from '@/api/edu/teacher'
export default {
  data() {
    return {
      teacher: {
        name: '',
        sort: 0,
        level: 1,
        career: '',
        intro: '',
        avatar: ''
      },
      oldAvatar: '',
      BASE_API: process.env.VUE_APP_BASE_API,
      saveBtnDisabled: false // 保存按钮是否禁用,
    }
  },

  //页面渲染成功
  created() {
    if (this.$route.params.id) {
      this.fetchDataById(this.$route.params.id)
    }
  },

  methods: {
    // 根据id查询记录
    fetchDataById(id) {
      teacherApi.getById(id).then(response => {
        this.teacher = response.data.record
        this.oldAvatar = this.teacher.avatar
      })
    },
    saveOrUpdate() {
      this.saveBtnDisabled = true
      if (!this.teacher.id) {
        this.saveData()
      } else {
        this.updateData()
        if (this.oldAvatar != this.teacher.avatar) {
          this.remove(this.oldAvatar)
        }
      }
    },

    // 新增数据
    saveData() {
      // debugger
      teacherApi.save(this.teacher).then(response => {
        this.$message({
          type: 'success',
          message: response.message
        })
        this.$router.push('/edu/teacher/list')
      })
    },

    // 根据id更新记录
    updateData() {
      // 数据的获取
      teacherApi.updateById(this.teacher).then(response => {
        this.$message({
          type: 'success',
          message: response.message
        })
        this.$router.push('/edu/teacher/list')
      })
    },

    handleAvatarSuccess(res, file) {
      console.log('file.row' + file.raw)
      this.teacher.avatar = URL.createObjectURL(file.raw)
    },

    fileUploadSuccess(response) {
      if (response.code === 0) {
        this.$message.success('上传成功')
        this.teacher.avatar = response.data.url
      } else {
        this.$message.error(response.message)
      }
    },
    remove(url) {
      teacherApi.remove(url).then(response => {})
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>
